﻿using MediatR;
using SY.BasicData.Api.Read.Applications.Cultivation.Command;
using SY.BasicData.Domain.Cultivation;
using SY.BasicData.ErrorCode;
using SY.BasicData.Infrastructures;

namespace SY.BasicData.Api.Read.Applications.Cultivation.CommandHandler
{
    public class GetCultivationQueryCommandHandler : IRequestHandler<GetCultivationQueryCommand, ApiResult<CultivationModel>>
    {
        private readonly IBaseRepository<CultivationModel> baseRepository;

        public GetCultivationQueryCommandHandler(IBaseRepository<CultivationModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public Task<ApiResult<CultivationModel>> Handle(GetCultivationQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<CultivationModel> result=new ApiResult<CultivationModel>();
            result.Code = ApiEnum.Success;
            result.Message = "成功";
            var list = baseRepository.GetAll().Where(x => x.Batch == request.Batch).OrderByDescending(x => x.Id).FirstOrDefault();
            if (list == null)
            {
                result.Message = "错误";
                result.Code = ApiEnum.Fail;
                return Task.FromResult(result);
            }
            result.Data = list;
            return Task.FromResult(result);
        }
    }
}
